﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using MatchResults.ViewModel;
using System.Collections.Generic;
using MatchResults.Model;
using SportInfoService.Helpers;
using System.Linq;

namespace MatchResults.Services
{
    public class LiveMatchResultService : ServiceBase<LiveMatchResultViewModel>
    {
        static int T = 1;
        protected override Uri GetUri(object param)
        {
            string MatchResultUrl = "http://www.iddaa.com.tr/XML/SkorYayin/index.htm?foraccess=KSsec654";

            return new Uri(MatchResultUrl);
        }

        protected override IEnumerable<LiveMatchResultViewModel> ParseData(string data, object param)
        {
            if (data.IndexOf("<Livescores") > 0)
            {
                data = data.Substring(data.IndexOf("<Livescores"));
                data = data.Substring(0, data.IndexOf("<!--"));
                Livescores livescores = XMLHelper.Deserialize<Livescores>(data, Encoding.UTF8);
                if (App.LiveResultModel == null)
                {
                    App.LiveResultModel = new List<LiveMatchResultViewModel>();
                }
                if (livescores.Match != null)
                {
                    for (int i = 0; i < livescores.Match.Count; i++)
                    {
                        if (App.LiveResultModel.Any(c => c.MatchId == livescores.Match[i].matchid))
                        {
                            var match = App.LiveResultModel.First(c => c.MatchId == livescores.Match[i].matchid);
                            match.Statusid = livescores.Match[i].statusid;
                            match.Takimlar = livescores.Match[i].takimlar;
                            match.Lig = livescores.Match[i].lig;
                            match.Scorehalfhome = livescores.Match[i].scorehalfhome;
                            match.Scorehalfaway = livescores.Match[i].scorehalfaway;
                            match.Scorecurrenthome = livescores.Match[i].scorecurrenthome;
                            match.Scorecurrentaway = livescores.Match[i].scorecurrentaway;
                            match.Matchstart = livescores.Match[i].matchstart;
                            match.Secondhalfstart = livescores.Match[i].secondhalfstart;
                            match.Minute = match.Statusid == 1 ? livescores.Match[i].tarihmac : (match.Statusid == 2 ? livescores.Match[i].ilkyaridk : (match.Statusid == 10 ? "D.A" : livescores.Match[i].ikiyaridk));
                            

                        }
                        else
                        {
                            App.LiveResultModel.Add(new LiveMatchResultViewModel
                            {
                                MatchId = livescores.Match[i].matchid,
                                Statusid = livescores.Match[i].statusid,
                                Takimlar = livescores.Match[i].takimlar,
                                Lig = livescores.Match[i].lig,
                                Scorehalfhome = livescores.Match[i].scorehalfhome,
                                Scorehalfaway = livescores.Match[i].scorehalfaway,
                                Scorecurrenthome = livescores.Match[i].scorecurrenthome,
                                Scorecurrentaway = livescores.Match[i].scorecurrentaway,
                                Matchstart = livescores.Match[i].matchstart,
                                Secondhalfstart = livescores.Match[i].secondhalfstart,
                                Minute = livescores.Match[i].statusid == 1 ? livescores.Match[i].tarihmac : (livescores.Match[i].statusid == 2 ? livescores.Match[i].ilkyaridk : (livescores.Match[i].statusid == 10 ? "D.A" : livescores.Match[i].ikiyaridk)) 
                            });
                        }

                    }
                }
                if (OnComplete != null)
                {
                    OnComplete(this, new EventArgs());
                }
            }
            return App.LiveResultModel;
            //else
            //{
            //    //return new List<LiveMatchResultViewModel>();
            //    T = -T;
            //    if (App.LiveResultModel == null)
            //    {
            //        App.LiveResultModel = new List<LiveMatchResultViewModel>();
            //    }

            //    List<LiveMatchResultViewModel> result = new List<LiveMatchResultViewModel>();
            //    for (int i = 0; i < 2; i++)
            //    {
            //        if (App.LiveResultModel.Any(c => c.MatchId == 1))
            //        {
            //            var match = App.LiveResultModel.First(c => c.MatchId == 1);
            //            match.Statusid = 2;
            //            match.Takimlar = "a-b";
            //            match.Lig = "tst";
            //            match.Scorehalfhome = "0";
            //            match.Scorehalfaway = "0";
            //            match.Scorecurrenthome = T > 0 ? "0" : "1";
            //            match.Scorecurrentaway = "0";
            //            match.Matchstart = "0";
            //            match.Secondhalfstart = "0";
            //            match.Ilkyaridk = "60";
            //            match.Ikiyaridk = "70";

            //        }
            //        else
            //        {
            //            App.LiveResultModel.Add(new LiveMatchResultViewModel
            //            {
            //                MatchId = 1,
            //                Statusid = 2,
            //                Takimlar = "a-b",
            //                Lig = "tst",
            //                Scorehalfhome = "0",
            //                Scorehalfaway = "0",
            //                Scorecurrenthome = T > 0 ? "0" : "1",
            //                Scorecurrentaway = "0",
            //                Matchstart = "0",
            //                Secondhalfstart = "0",
            //                Ilkyaridk = "60",
            //                Ikiyaridk = "70",

            //            });
            //        }
            //    }
            //    return App.LiveResultModel;
            //}
        }

        protected override void AddWebClientHeaders(WebClient wc)
        {
            wc.Headers["Accept"] = "*/*";
            wc.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.3";
            wc.Headers["Accept-Encoding"] = "gzip,deflate,sdch";
            wc.Headers["Accept-Language"] = "en-US,en;q=0.8";
            wc.Headers["Cache-Control"] = "no-cache";
            wc.Headers["Host"] = "www.iddaa.com.tr";
            wc.Headers["Referer"] = "http://www.iddaa.com.tr/BITENMACLAR/FUTBOL/index.htm?PROGRAMID=1";
            wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5";
            wc.Encoding = Encoding.UTF8;
        }

        public override event EventHandler OnComplete;
    }
}
